// pages/register/register.js
const util = require('../../utils/util');
const api = require('../../request/api');
import Toast from '../../vant/weapp/toast/toast';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showShade: false,
    fileList: [],
    fileListMsg: [],
    schoolId: '',
    realName: '',
    grade: '',
    colleges: '',
    phone: '',
    registerPhoto1: '',
    registerPhoto2: '',
    showBox: false,
    departmentList:[],
    department:'',
  },
  onConfirm(event) {
    console.log(event.detail.value.text)
    console.log(event.detail.value.value)
    this.setData({
      department:event.detail.value.text,
      schoolId:event.detail.value.value,
      showBox: false
    })
  },
  onCancel() {
    this.setData({
      showBox: false
    });
  },
  getDepartment() {
    util.post(api.school.query,{}).then(res => {
      // console.log(res);
      if (res.code == 200) {
        var list = res.data.list;
        var alist = [];
        list.forEach(e => {
          alist.push({
            text: e.schoolName,
            value: e.schoolId
          })
        });
        console.log(list);
        console.log(alist);
        this.setData({
          departmentList: alist
        })
      } else {
        Toast.fail(res.message)
      }
    }).catch((err) => {

    });
  },
  clickInput() {
    console.log(this.data.departmentList);
    this.setData({
      showBox: true
    });
  },
  afterRead(event) {
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    this.setData({
      showShade: true
    });
    Toast.loading({
      duration: 0,
      message: '上传中...',
      forbidClick: true,
    });
    wx.uploadFile({
      url: api.register.submint,
      filePath: file.url,
      name: 'file',
      formData: {
        user: 'test'
      },
      header: {
        "Authorization": wx.getStorageSync("userToekn")
      },
      timeout: 50000,
      success: (res) => {
        Toast.clear();
        this.setData({
          showShade: false
        });
        console.log(res)
        var data = JSON.parse(res.data);
        console.log(data);
        var url = api.register.photo + '/' + data.data.realName;
        console.log(data.data.realName)
        console.log(url)
        const {
          fileList = []
        } = this.data;
        const {
          fileListMsg = []
        } = this.data;
        fileList.push({
          ...file,
          url: url
        });
        fileListMsg.push({
          data: data.data
        });
        this.setData({
          fileList,
          fileListMsg
        });
        console.log(fileList)
        console.log(fileListMsg)
      },
      fail: (res) => {},
      complete: (res) => {},
    }).finally(() => {
      Toast.clear();
      this.setData({
        showShade: false
      });
    });
  },
  returnFileList() {
    return this.data.fileList;
  },
  beforeRead(event) {
    const {
      file,
      callback
    } = event.detail;
    console.log(file)
    callback(file.type === 'image');
  },
  snbmitInfo(e) {

    var info = {};
    info.registerPhoto1 = this.data.fileListMsg[0].data.id;
    info.registerPhoto2 = this.data.fileListMsg[1].data.id;
    info.realName = this.data.realName;
    info.colleges = this.data.colleges;
    info.grade = this.data.grade;
    info.schoolId = this.data.schoolId;
    info.phone = this.data.phone;
    info.userId = wx.getStorageSync('userInfo').userId;

    console.log(info);
    this.setData({
      showShade: true
    });
    Toast.loading({
      duration: 0,
      message: '提交中...',
      forbidClick: true,
    });
    util.post(api.register.append, info).then(res => {
      Toast.clear();
      this.setData({
        showShade: false
      });
      if (res.code == 200) {
        Toast.success("提交成功");
        setTimeout(function(){
          wx.navigateTo({
            url: `../userInfo/userInfo`
          })
        },2000)
      } else {
        Toast.fail(res.message)
      }
    }).catch((err) => {
      Toast.clear();
      this.setData({
        showShade: false
      });
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.getDepartment();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})